﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
    internal static class GenParse_ErrorCode
    {
        #region private constants

        private const byte GeneralByte = 0x00;
        private const byte UnknownByte = 0x01;
        private const byte NotFulfillableByte = 0x02;
        private const byte NegativeConfirmByte = 0x03;
        private const byte WrongSecurityCodeByte = 0x04;
        private const byte AddressOutOfRangeByte = 0x10;
        private const byte FunctionNotSupportedByAddressByte = 0x18;
        private const byte DataOutOfRangeByte = 0x20;
        private const byte ChecksumErrorByte = 0x80;

        #endregion private constants

        #region internal methods

        internal static byte GenerateByte(eErrorCode errorCode)
        {
            byte errorCodeByte = 0x0;

            switch (errorCode)
            {
                case eErrorCode.AddressOutOfRange:
                    errorCodeByte = AddressOutOfRangeByte;
                    break;
                case eErrorCode.ChecksumError:
                    errorCodeByte = ChecksumErrorByte;
                    break;

                case eErrorCode.DataOutOfRange:
                    errorCodeByte = DataOutOfRangeByte;
                    break;
                case eErrorCode.FunctionNotSupportedByAddress:
                    errorCodeByte = FunctionNotSupportedByAddressByte;
                    break;
                case eErrorCode.General:
                    errorCodeByte = GeneralByte;
                    break;
                case eErrorCode.NegativeConfirm:
                    errorCodeByte = NegativeConfirmByte;
                    break;
                case eErrorCode.NotFulfillable:
                    errorCodeByte = NotFulfillableByte;
                    break;
                case eErrorCode.Unknown:
                    errorCodeByte = UnknownByte;
                    break;
                case eErrorCode.WrongSecurityCode:
                    errorCodeByte = WrongSecurityCodeByte;
                    break;
                default:
                    // TODO handle error (unknown value for eErrorCode)
                    break;
            }

            return errorCodeByte;
        }

        internal static bool ParseByte(byte errorCodeByte, out eErrorCode errorCode)
        {
            // set default value
            bool bRetVal = true;
            errorCode = eErrorCode.Unknown;

            switch (errorCodeByte)
            {
                case GeneralByte:
                    errorCode = eErrorCode.General;
                    break;                
                case UnknownByte:
                    errorCode = eErrorCode.Unknown;
                    break;
                case NotFulfillableByte:
                    errorCode = eErrorCode.NotFulfillable;
                    break;
                case NegativeConfirmByte:
                    errorCode = eErrorCode.NegativeConfirm;
                    break;
                case WrongSecurityCodeByte:
                    errorCode = eErrorCode.WrongSecurityCode;
                    break;
                case AddressOutOfRangeByte:
                    errorCode = eErrorCode.AddressOutOfRange;
                    break;
                case FunctionNotSupportedByAddressByte:
                    errorCode = eErrorCode.FunctionNotSupportedByAddress;
                    break;
                case DataOutOfRangeByte:
                    errorCode = eErrorCode.DataOutOfRange;
                    break;
                case ChecksumErrorByte:
                    errorCode = eErrorCode.ChecksumError;
                    break;
                default:
                    // TODO handle error (unknown value for errorCodeByte)
                    bRetVal = false;
                    break;
            }

            return bRetVal;
        }

        #endregion internal methods
    }
}
